package com.craftplay.service;


import com.craftplay.dao.LoveMapper;
import com.craftplay.dao.UnitMapper;
import com.craftplay.model.BehaveModel;
import com.craftplay.model.Love;
import com.craftplay.model.Unit;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class LoveService {

    @Autowired
    private LoveMapper loveMapper;

    @Autowired
    private BehaveService behaveService;

    @Autowired
    private UnitMapper unitMapper;


    /**
     * 我的喜欢
     *
     * @return
     */
    public List<Love> myLove(Integer userId) {
        return loveMapper.myLove(userId);
    }

    /**
     * 新增喜欢
     *
     * @return
     */
    public int addLove(Love love) {
        //判断是否已经喜欢
        Love isLove = loveMapper.isLove(love.getUnitId(), love.getUserId());
        if (isLove == null) {
            Unit unit = unitMapper.selectByPrimaryKey(love.getUnitId());
            if (unit == null) {
                return 0;
            }
            BehaveModel behaveModel = new BehaveModel();
            behaveModel.setUserId(unit.getUserId());
            behaveModel.setBuserId(love.getUserId());
            behaveModel.setUnitId(unit.getId());
            behaveModel.setBehave("喜欢");
            behaveModel.setCreateTime(new Date());
            behaveService.insertBehave(behaveModel);

            return loveMapper.addLove(love);
        }
        return 1;
    }

    /**
     * 有多少喜歡
     *
     * @param unitId
     * @return
     */
    public int numberLove(Integer unitId) {
        return loveMapper.numberLove(unitId);
    }


    /**
     * 是否已喜歡
     *
     * @return
     */
    public Boolean isLove(Integer unitId, Integer userId) {
        Love love = loveMapper.isLove(unitId, userId);
        if (love == null) {
            return false;
        }
        return true;
    }

    /**
     * 取消喜欢
     *
     * @param love
     * @return
     */
    public int delLove(Love love) {
        return loveMapper.delLove(love);
    }
}
